<?php

	class Nhacungcap_model extends CI_Model
	{
		function LayNCC1() // đang hợp tác OK
		{
			$this->db->select('ncc.*, ks.Ten as tenks, ks.MaKS as maks');
			$this->db->from('NhaCungCap as ncc');
			$this->db->join('KhachSan as ks','ks.NhaCungCap = ncc.MaNCC');
			$this->db->where('TinhTrangTaiKhoan = 1');
			$this->db->limit(20, $this->uri->segment(3));
			$this->db->order_by("MaNCC","DESC");
			$query = $this->db->get();
			
			$total_query = "select * from NhaCungCap where TinhTrangTaiKhoan = 1";

			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nhacungcap/admin_index';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = 20;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		function LayNCC2() //ngừng hợp tác OK
		{
			$this->db->select('ncc.*, ks.Ten as tenks, ks.MaKS as maks');
			$this->db->from('NhaCungCap as ncc');
			$this->db->join('KhachSan as ks','ks.NhaCungCap = ncc.MaNCC');
			$this->db->where('TinhTrangTaiKhoan = 0');
			$this->db->limit(20, $this->uri->segment(3));
			$this->db->order_by("MaNCC","desc");
			$query = $this->db->get();

			$total_query = "select * from NhaCungCap where TinhTrangTaiKhoan = 0";
			
			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nhacungcap/nccngunghoptac';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = 20;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		
		function LayDKMoi() // ncc đăng ký mới OK
		{
			$this->db->select('KhachSan.Ten as tenks,NhaCungCap.Ten as tenncc, NhaCungCap.MaNCC as mancc');
			$this->db->from('NhaCungCap');
			$this->db->join('KhachSan','KhachSan.NhaCungCap = NhaCungCap.MaNCC');
			$this->db->where('TinhTrangDuyet = 0');
			$this->db->limit(20, $this->uri->segment(3));
			$this->db->order_by("MaNCC","desc");
			$query = $this->db->get();
			$total_query = "select * from NhaCungCap where TinhTrangDuyet = 0";
			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nhacungcap/dangkymoi';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();				
			$config['per_page'] = 20;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		
		function CapNhatTinhTrangDuyet($mancc)
		{
			$state = 1;
			$this->db->update('NhaCungCap', array('TinhTrangDuyet' => $state), array('MaNCC' => $mancc));
			return $this->db->affected_rows();
		}
		
		function LayNCCDuyet() // ncc duyet OK
		{
			$this->db->select('KhachSan.Ten as tenks,NhaCungCap.Ten as tenncc, NhaCungCap.MaNCC as mancc');
			$this->db->from('NhaCungCap');
			$this->db->join('KhachSan','KhachSan.NhaCungCap = NhaCungCap.MaNCC');
			$this->db->where('TinhTrangDuyet = 1 AND TinhTrangTaiKhoan is null');
			$this->db->limit(20, $this->uri->segment(3));
			$this->db->order_by("MaNCC","desc");
			$query = $this->db->get();
			$total_query = "select * from NhaCungCap where TinhTrangDuyet = 1 AND TinhTrangTaiKhoan is null";
			
			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nhacungcap/nccduyet';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = 20;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		
		function HopTac($mancc) // duyet hop tac
		{
			$state = 1;
			$this->db->update('NhaCungCap', array('TinhTrangTaiKhoan' => $state), array('MaNCC' => $mancc));
			return $this->db->affected_rows();
		}
		
		function SetPass($mancc)
		{
			$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
			$pass = array(); //remember to declare $pass as an array
			$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
			for ($i = 0; $i < 8; $i++)
			{
				$n = rand(0, $alphaLength);
				$pass[] = $alphabet[$n];
    		}
			$pass = implode($pass);
			$pass1 = md5($pass);
			$this->db->update('NhaCungCap', array('Password' => $pass1), array('MaNCC' => $mancc));
			return $pass;
		}
		
		function SetUsername($mancc)
		{
			$ncc = $this->GetARecord($mancc);
			$ten = NULL;
			if($ncc)
			{
				foreach($ncc as $row)
					$ten = $row->Ten;
				$temp = array();
				$temp = explode(' ',$ten);
				$i = 0;
				$temp2 = array();
				foreach($temp as $t)
				{
					$temp2[$i] = substr($t,0,1);
					$i++;
				}
				$i--;
				$temp2[$i] = $temp[$i];
				$name = "";
				foreach($temp2 as $t)
				{
					$name .= $t;
				}
				$username = strtolower($this->RemoveSign($name));
				$this->db->update('NhaCungCap', array('Username' => $username), array('MaNCC' => $mancc));
				return $username;
			}
			return $name;
		}
		
		function RemoveSign($str)
		{
			$coDau=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă","ằ","ắ"
			,"ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề","ế","ệ","ể","ễ","ì","í","ị","ỉ","ĩ",
			"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
			,"ờ","ớ","ợ","ở","ỡ",
			"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
			"ỳ","ý","ỵ","ỷ","ỹ",
			"đ",
			"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
			,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
			"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
			"Ì","Í","Ị","Ỉ","Ĩ",
			"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
			,"Ờ","Ớ","Ợ","Ở","Ỡ",
			"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
			"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
			"Đ","ê","ù","à");
			$khongDau=array("a","a","a","a","a","a","a","a","a","a","a"
			,"a","a","a","a","a","a",
			"e","e","e","e","e","e","e","e","e","e","e",
			"i","i","i","i","i",
			"o","o","o","o","o","o","o","o","o","o","o","o"
			,"o","o","o","o","o",
			"u","u","u","u","u","u","u","u","u","u","u",
			"y","y","y","y","y",
			"d",
			"A","A","A","A","A","A","A","A","A","A","A","A"
			,"A","A","A","A","A",
			"E","E","E","E","E","E","E","E","E","E","E",
			"I","I","I","I","I",
			"O","O","O","O","O","O","O","O","O","O","O","O"
			,"O","O","O","O","O",
			"U","U","U","U","U","U","U","U","U","U","U",
			"Y","Y","Y","Y","Y",
			"D","e","u","a");
			return str_replace($coDau,$khongDau,$str);
		}
		
		function KhongHopTac($mancc) // duyet khong hop tac
		{
			$this->db->delete('NhaCungCap',array('MaNCC' => $mancc));
			$this->db->delete('KhachSan',array('NhaCungCap' => $mancc));
			return $this->db->affected_rows();
		}
		
		
		function HopTacTB($mancc)
		{
			$state = NULL;
			$this->db->update('NhaCungCap', array('TinhTrangTaiKhoan' => $state),array('MaNCC' => $mancc));
			return $this->db->affected_rows();
		}
		
		function GetARecord($mancc)
		{
			$query = $this->db->get_where('NhaCungCap',array('MaNCC' => $mancc));
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;
		}
		
		function layemailncc($mancc)
		{
			$ncc = $this->GetARecord($mancc);
			$email = NULL;
			if($ncc)
			{
				foreach($ncc as $row)
					$email = $row->Email;
				return $email;
			}
			return $email;
		}
		
		function LayUsername($mancc)
		{
			$ncc = $this->GetARecord($mancc);
			$username = NULL;
			if($ncc)
			{
				foreach($ncc as $row)
					$username = $row->Username;
				return $username;
			}
			return $username;
		}
		
		function LayNhaCungCap($mancc)
		{
			$strSQL = "SELECT ncc.*, ks.Ten as tenks, ks.MaKS as maks
						FROM NhaCungCap as ncc, KhachSan as ks
						WHERE ncc.MaNCC = '$mancc' AND ks.NhaCungCap = ncc.MaNCC";
			$query = $this->db->query($strSQL);		
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;		
		}
		
		function LayChiTietDuyetNhaCungCap($mancc)
		{
			$strSQL = "SELECT ncc.*, ks.Ten as tenks, ks.MaKS as maks, ks.SoNha as sn, ks.Duong as d, ks.Quan as q, ks.Tinh_ThanhPho tp, ks.QuocGia as qg, ks.DienThoai as dt, ks.Email as em, ks.TieuChuan as tc, ks.QuyDinh as qd, ks.ThongTin as tt, ks.VAT as vat, ks.PhiDichVu as p, ks.HoaHong as hh
						FROM NhaCungCap as ncc, KhachSan as ks
						WHERE ncc.MaNCC = '$mancc' AND ks.NhaCungCap = ncc.MaNCC";
			$query = $this->db->query($strSQL);		
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;		
		}
		
		function xuatfile($mancc)
		{
			$file_name = "application/file_print/nhacungcap_" .$mancc . ".txt";			
			$file = fopen( $file_name,"w");
			$strSQL = "SELECT ncc.*, ks.Ten as tenks, ks.MaKS as maks, ks.SoNha as sn, ks.Duong as d, ks.Quan as q, ks.Tinh_ThanhPho tp, ks.QuocGia as qg, ks.DienThoai as dt, ks.Email as em, ks.TieuChuan as tc, ks.QuyDinh as qd, ks.ThongTin as tt, ks.VAT as vat, ks.PhiDichVu as p, ks.HoaHong as hh
						FROM NhaCungCap as ncc, KhachSan as ks
						WHERE ncc.MaNCC = '$mancc' AND ks.NhaCungCap = ncc.MaNCC";
			$query = $this->db->query($strSQL);	
			/*if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;*/
				
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$mancc = $row->MaNCC;
					$tenncc = $row->Ten;
					$diachi = $row->DiaChi;		
					$dienthoai = $row->DienThoai;
					$email = $row->Email;
					$fax = $row->Fax;			
					$masothue = $row->MaSoThue;
					$tinhtrang = $row->TinhTrangTaiKhoan;
					$tenks = $row->tenks;
					$maks = $row->maks;
					$diachiks = 'Số ' . $row->sn . ', đường ' . $row->d . ', quận ' . $row->q . ', tỉnh/thành phố ' . $row->tp . ', nước ' . $row->qg;
					$dienthoaiks = $row->dt;
					$emailks = $row->em;
					$tieuchuan = $row->tc;
					$quydinh = $row->qd;
					$thongtin = $row->tt;
					$vat = $row->vat;
					$phidichvu = $row->p;
					$hoahong = $row->hh;
					$str = "THÔNG TIN NHÀ CUNG CẤP\r\nTên: $tenncc\r\nĐịa chỉ: $diachi\r\nĐiện thoại: $dienthoai\r\nEmail: $email\r\nFax: $fax\r\nMã số thuế: $masothue\r\n\r\nTHÔNG TIN KHÁCH SẠN\r\nTên khách sạn: $tenks\r\nĐịa chỉ: $diachiks\r\nĐiện thoại: $dienthoaiks\r\nTiêu chuẩn: $tieuchuan\r\nThông tin: $thongtin\r\nQuy định: $quydinh\r\nVAT: $vat%\r\nPhí dịch vụ: $phidichvu.000VNĐ\r\nHoa hồng: $hoahong%";
					$temp = fwrite($file, $str);
					if($temp === false)
					{
						fclose($file);
						return false;
					}
					$this->load->helper('download');
					$data = file_get_contents($file_name);
					force_download($file_name, $data);
				}
			}
			fclose($file);
			return true;
		}
		
		function LayChiTietNhaCungCap($mancc)
		{
			$strSQL = "SELECT *
						FROM NhaCungCap
						WHERE MaNCC = '$mancc'";
			$query = $this->db->query($strSQL);		
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;		
		}
		
		function CapNhat($data, $mancc)
		{
			$this->db->update('NhaCungCap', $data, array('MaNCC' => $mancc)); 
			return $this->db->affected_rows();			
		}
		
		function TimNCCHH($key)
		{
			$post_per_page = 20;
			$num = 0;
			if($this->uri->segment(3))
				$num = $this->uri->segment(3);
				
			$strSQL = "SELECT ncc.*, ks.Ten as tenks, ks.MaKS as maks
						FROM NhaCungCap as ncc, KhachSan as ks
						WHERE ks.NhaCungCap = ncc.MaNCC AND ncc.TinhTrangTaiKhoan = 1
						AND (ncc.MaNCC = '$key' OR ncc.Ten LIKE '%$key%' OR ks.Ten LIKE '%$key%')
						ORDER BY ncc.MaNCC desc
						LIMIT $num, $post_per_page";
			$query = $this->db->query($strSQL);								
			
			$total_query = "SELECT ncc.*, ks.Ten as tenks, ks.MaKS as maks
						FROM NhaCungCap as ncc, KhachSan as ks
						WHERE ks.NhaCungCap = ncc.MaNCC AND ncc.TinhTrangTaiKhoan = 1
						AND (ncc.MaNCC = '$key' OR ncc.Ten LIKE '%$key%' OR ks.Ten LIKE '%$key%')";	
			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nhacungcap/admin_index';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function TimNCCDKM($key)
		{
			$post_per_page = 20;
			$num = 0;
			if($this->uri->segment(3))
				$num = $this->uri->segment(3);
				
			$strSQL = "SELECT ncc.MaNCC as mancc, ncc.Ten as tenncc, ks.Ten as tenks
						FROM NhaCungCap as ncc, KhachSan as ks
						WHERE ks.NhaCungCap = ncc.MaNCC AND ncc.TinhTrangTaiKhoan IS NULL AND ncc.TinhTrangDuyet = 0
						AND (ncc.MaNCC = '$key' OR ncc.Ten LIKE '%$key%' OR ks.Ten LIKE '%$key%')
						ORDER BY ncc.MaNCC desc
						LIMIT $num, $post_per_page";
			$query = $this->db->query($strSQL);								
			
			$total_query = "SELECT ncc.*, ks.Ten as tenks, ks.MaKS as maks
						FROM NhaCungCap as ncc, KhachSan as ks
						WHERE ks.NhaCungCap = ncc.MaNCC AND ncc.TinhTrangTaiKhoan IS NULL AND ncc.TinhTrangDuyet = 0
						AND (ncc.MaNCC = '$key' OR ncc.Ten LIKE '%$key%' OR ks.Ten LIKE '%$key%')";	
			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nhacungcap/admin_index';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function TimNCCD($key)
		{
			$post_per_page = 20;
			$num = 0;
			if($this->uri->segment(3))
				$num = $this->uri->segment(3);
				
			$strSQL = "SELECT ncc.MaNCC as mancc, ncc.Ten as tenncc, ks.Ten as tenks
						FROM NhaCungCap as ncc, KhachSan as ks
						WHERE ks.NhaCungCap = ncc.MaNCC AND ncc.TinhTrangTaiKhoan IS NULL AND ncc.TinhTrangDuyet = 1
						AND (ncc.MaNCC = '$key' OR ncc.Ten LIKE '%$key%' OR ks.Ten LIKE '%$key%')
						ORDER BY ncc.MaNCC desc
						LIMIT $num, $post_per_page";
			$query = $this->db->query($strSQL);								
			
			$total_query = "SELECT ncc.*, ks.Ten as tenks, ks.MaKS as maks
						FROM NhaCungCap as ncc, KhachSan as ks
						WHERE ks.NhaCungCap = ncc.MaNCC AND ncc.TinhTrangTaiKhoan IS NULL AND ncc.TinhTrangDuyet = 1
						AND (ncc.MaNCC = '$key' OR ncc.Ten LIKE '%$key%' OR ks.Ten LIKE '%$key%')";	
			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nhacungcap/admin_index';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function TimNCCN($key)
		{
			$post_per_page = 20;
			$num = 0;
			if($this->uri->segment(3))
				$num = $this->uri->segment(3);
				
			$strSQL = "SELECT ncc.*, ks.Ten as tenks, ks.MaKS as maks
						FROM NhaCungCap as ncc, KhachSan as ks
						WHERE ks.NhaCungCap = ncc.MaNCC AND ncc.TinhTrangTaiKhoan = 0
						AND (ncc.MaNCC = '$key' OR ncc.Ten LIKE '%$key%' OR ks.Ten LIKE '%$key%')
						ORDER BY ncc.MaNCC desc
						LIMIT $num, $post_per_page";
			$query = $this->db->query($strSQL);								
			
			$total_query = "SELECT ncc.*, ks.Ten as tenks, ks.MaKS as maks
						FROM NhaCungCap as ncc, KhachSan as ks
						WHERE ks.NhaCungCap = ncc.MaNCC AND ncc.TinhTrangTaiKhoan  = 0
						AND (ncc.MaNCC = '$key' OR ncc.Ten LIKE '%$key%' OR ks.Ten LIKE '%$key%')";	
			$this->load->library('pagination');
			$config['base_url'] = base_url() . 'index.php/nhacungcap/admin_index';
			$config['total_rows'] = $this->db->query($total_query)->num_rows();		
			$config['per_page'] = $post_per_page;
			$this->pagination->initialize($config);	
						
			if($query->num_rows() > 0)
			{
				foreach($query->result() as $row)
				{
					$data[] = $row;
				}
				return $data;
			}
			return NULL;			
		}
		
		function LayTenNCC_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ncc.Ten
						FROM NhaCungCap ncc
						WHERE ncc.TinhTrangTaiKhoan = 1
						AND ncc.Ten LIKE '%$key%'
						ORDER BY ncc.Ten ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;
		}
		
		function LayTenKhachSan_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ks.Ten
						FROM KhachSan ks
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan = 1
						AND ks.Ten LIKE '%$key%'
						ORDER BY ks.Ten ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;		
		}
		
		function LayTenNCCdkmoi_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ncc.Ten
						FROM NhaCungCap ncc
						WHERE ncc.TinhTrangDuyet = 0
						AND ncc.Ten LIKE '%$key%'
						ORDER BY ncc.Ten ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;
		}
		
		function LayTenKhachSandkmoi_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ks.Ten
						FROM KhachSan ks
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangDuyet = 0
						AND ks.Ten LIKE '%$key%'
						ORDER BY ks.Ten ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;		
		}
		
		function LayTenNCCduyet_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ncc.Ten
						FROM NhaCungCap ncc
						WHERE ncc.TinhTrangDuyet = 1 AND ncc.TinhTrangTaiKhoan IS NULL
						AND ncc.Ten LIKE '%$key%'
						ORDER BY ncc.Ten ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;
		}
		
		function LayTenKhachSanduyet_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ks.Ten
						FROM KhachSan ks
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangDuyet = 0 AND ncc.TinhTrangTaiKhoan IS NULL
						AND ks.Ten LIKE '%$key%'
						ORDER BY ks.Ten ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;		
		}
		
		function LayTenNCCngung_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ncc.Ten
						FROM NhaCungCap ncc
						WHERE ncc.TinhTrangTaiKhoan = 0
						AND ncc.Ten LIKE '%$key%'
						ORDER BY ncc.Ten ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;
		}
		
		function LayTenKhachSanngung_ajax($key)
		{
			$strSQL = "SELECT DISTINCT ks.Ten
						FROM KhachSan ks
						JOIN NhaCungCap ncc ON ks.NhaCungCap = ncc.MaNCC
						WHERE ncc.TinhTrangTaiKhoan = 0
						AND ks.Ten LIKE '%$key%'
						ORDER BY ks.Ten ASC";
	        $query = $this->db->query($strSQL);	

	        if ($query->num_rows() > 0) 
			{
        	    foreach ($query->result() as $row) 
				{
	                $data[] = $row;
    	        }
        	    return $data;
	        }
    	    return NULL;		
		}



    function Them($data)
	{
        $this->db->insert('NhaCungCap', $data);
        return $this->db->affected_rows();
    }

    function LayNCCMoiNhat()
	{
        $strSQL = "SELECT *
    					FROM NhaCungCap
    					ORDER BY MaNCC DESC
    					LIMIT 1";
        $query = $this->db->query($strSQL);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $mancc = $row->MaNCC;
            }
            return $mancc;
        }
        return NULL;
    }
		
		function CapNhatTinhTrang($mancc) // Hủy nhà cung cấps - ngừng hợp tác
		{
			$state = 0;
			$this->db->update('NhaCungCap', array('TinhTrangTaiKhoan' => $state), array('MaNCC' => $mancc));
			return $this->db->affected_rows();	
		}
			function DuyetLai($mancc)
		{
			$state1 = 1;
			$state2 = NULL;
			$this->db->update('NhaCungCap', array('TinhTrangDuyet' => $state1, 'TinhTrangTaiKhoan' => $state2), array('MaNCC' => $mancc));
			return $this->db->affected_rows();	
		}
	
		
	
    function CheckLogin($username, $password) {
        $password = md5($password);
        $query = $this->db->get_where('NhaCungCap', array('Username' => $username, 'Password' => $password, 'TinhTrangTaiKhoan' => 1));
        if ($query->num_rows() > 0)
            return 1;
        return 0;
    }

    function LayMaNCC($username) {
        $query = $this->db->get_where('NhaCungCap', array('Username' => $username));

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $mancc = $row->MaNCC;
            }
            return $mancc;
        }
        return NULL;
    }

}
?>